
/* global shoproot, DataTableConfig */

requirejs(['jquery', 'util', 'fancyBox', 'datatables', 'Spinner','layer','laytpl','laypage','WdatePicker','datetimepicker','xdate'], function ($, util, fancyBox, dataTables, Spinner,layer,laytpl,laypage,WdatePicker,xdate) {

	
	$(function () {
		
		 init_time_picker();
			  /**
	         *	初始化时间选择控件
	         **/
	     function init_time_picker(){
	        	$.datetimepicker.setLocale('zh');
	            $('#search_start').datetimepicker({
	                format: 'Y-m-d H:i:s'
	            });
	            $('#search_end').datetimepicker({
	            	format: 'Y-m-d H:i:s'
	            });
	         
	     }
	    var start= '';
	    var end ='';
	    $('#queryDate').click(function(){
	    	
	        start = $('#search_start').val();
		    end = $('#search_end').val();
		    
			ajax_load_data(start_page);
		 
	    });
	  	var date_ = new Date();  
	    $('#queryMonth').click(function(){
	  
	    	var firstDate = new Date();  
	    	firstDate.setDate(1); //第一天  
	    	var endDate = new Date(firstDate);  
	    	endDate.setMonth(firstDate.getMonth()+1);  
	    	endDate.setDate(0);  
	    	start = new XDate(firstDate).toString('yyyy-MM-dd')+" 00:00:00";
	    	end = new XDate(endDate).toString('yyyy-MM-dd')+" 24:00:00";
	    	
	    	//alert(start);
	    	ajax_load_data(start_page);
	    });
	
		//要渲染的容器对象
		var parent_container = $('.dTable tbody');
		//laytpl模版中的数据
		var template_container = $('#data_render').html();
		var total_container = $('#total');
		//异步加载数据的url
		
		var url = '?/OutCome/ajax_outcome_list';
		//每页显示的数目
		var pageSize = 10;
		//页码
		var page = 1;
		//起始页
		var start_page = 1;
		ajax_load_data(start_page);
        /**
		 * 异步加载数据
		 * status 状态码
		 * current_page 当前页码
		 */
    	function ajax_load_data(current_page){
    		layer.load(0);
    		if(current_page){
    			page = current_page;
    		}
 
    		$.post(url,{
    			page:page,
    			pageSize:pageSize,
    			start:start,
    			end:end
    			
    		},function(data){
    		
    			total_container.html("总金："+data.totalMoney);
    			//解析模版组装数据
    			laytpl(template_container).render(data,function(html){
    				
    				
    			    parent_container.html(html);
        		});
    			
    			//分页插件laypage的数据组装
    			var reminder = data.total%pageSize;
    			var devided = data.total/pageSize;
    			var totalPage =  reminder==0?devided:devided+1;
    			laypage({
    	            cont: 'page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
    	            pages: totalPage , //通过后台拿到的总页数
    	            curr: current_page || 1, //当前页
    	            skip: true, //是否开启跳页
    	            skin: '#AF0000',
    	            groups: 3, //连续显示分页数
    	            jump: function(obj, first){ //触发分页后的回调
    	                if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
    	                	ajax_load_data(obj.curr);
    	                }
    	            }
    	        });
    		});
    	}
    });
});